/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.devmng.service;

import com.thinkgem.jeesite.common.exception.MobileApiException;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.devmng.dao.TaskExecRecordDao;
import com.thinkgem.jeesite.modules.devmng.entity.TaskDeviceDevchkItem;
import com.thinkgem.jeesite.modules.devmng.entity.TaskExecRecord;
import com.thinkgem.jeesite.modules.devmng.entity.TaskExecRecordAtta;
import com.thinkgem.jeesite.modules.devmng.entity.TaskMain;
import com.thinkgem.jeesite.modules.devmng.vo.DevchkAnswerItemSubmitVo;
import com.thinkgem.jeesite.modules.devmng.vo.DevchkAnswerSubmitVo;
import com.thinkgem.jeesite.modules.devmng.vo.DevchkItemSubmitVo;
import com.thinkgem.jeesite.modules.devmng.vo.TaskExecRecordSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 任务执行记录Service
 *
 * @author panda
 * @version 2017-12-08
 */
@Service
@Transactional(readOnly = true)
public class TaskExecRecordService extends CrudService<TaskExecRecordDao, TaskExecRecord> {

    @Autowired
    private TaskExecRecordAttaService taskExecRecordAttaService;

    @Autowired
    private TaskDeviceDevchkItemService taskDeviceDevchkItemService;

    @Autowired
    @Lazy
    private TaskMainService taskMainService;

    @Transactional(readOnly = false)
    public void submit(TaskExecRecordSubmitVo taskExecRecordSubmitVo) {
        String taskMainId = taskExecRecordSubmitVo.getTaskMainId();
        TaskMain taskMain = taskMainService.get(taskMainId);
        if (!TaskMain.Status.PROCESSING.name().equals(taskMain.getTaskStatus())) {
            throw new MobileApiException("请先签收任务！");
        }

        String deviceMainId = taskExecRecordSubmitVo.getDeviceMainId();
        List<DevchkItemSubmitVo> devchkItemSubmitVoList = taskExecRecordSubmitVo.getDevchkItemSubmitVoList();
        for (DevchkItemSubmitVo devchkItemSubmitVo : devchkItemSubmitVoList) {
            String devchkItemId = devchkItemSubmitVo.getDevchkItemId();
            // 将当前检修项的状态修改为完成
            TaskDeviceDevchkItem taskDeviceDevchkItem = new TaskDeviceDevchkItem();
            taskDeviceDevchkItem.setTaskMainId(taskMainId);
            taskDeviceDevchkItem.setDeviceMainId(deviceMainId);
            taskDeviceDevchkItem.setDevchkItemId(devchkItemId);
            List<TaskDeviceDevchkItem> taskDeviceDevchkItemList = taskDeviceDevchkItemService.findList(taskDeviceDevchkItem);
            if (taskDeviceDevchkItemList.size() > 0) {
                TaskDeviceDevchkItem taskDeviceDevchkItem1 = taskDeviceDevchkItemList.get(0);
                taskDeviceDevchkItem1.setStatus(TaskDeviceDevchkItem.Status.CLOSE.name());
                taskDeviceDevchkItemService.save(taskDeviceDevchkItem1);
            }
            // 回答保存与附件保存
            List<DevchkAnswerSubmitVo> devchkAnswerSubmitVoList = devchkItemSubmitVo.getDevchkAnswerSubmitVoList();
            for (DevchkAnswerSubmitVo devchkAnswerSubmitVo : devchkAnswerSubmitVoList) {
                String answerType = devchkAnswerSubmitVo.getAnswerType();
                String devchkAnswerId = devchkAnswerSubmitVo.getDevchkAnswerId();
                List<DevchkAnswerItemSubmitVo> devchkAnswerItemSubmitVoList = devchkAnswerSubmitVo.getDevchkAnswerItemSubmitVoList();
                if (devchkAnswerItemSubmitVoList != null) {
                    // 回答保存
                    for (DevchkAnswerItemSubmitVo devchkAnswerItemSubmitVo : devchkAnswerItemSubmitVoList) {
                        String devchkAnswerItemId = devchkAnswerItemSubmitVo.getDevchkAnswerItemId();
                        BigDecimal numContent = devchkAnswerItemSubmitVo.getNumContent();
                        String textContent = devchkAnswerItemSubmitVo.getTextContent();
                        TaskExecRecord taskExecRecord = new TaskExecRecord();
                        taskExecRecord.setTaskMainId(taskMainId);
                        taskExecRecord.setDeviceMainId(deviceMainId);
                        taskExecRecord.setDevchkItemId(devchkItemId);
                        taskExecRecord.setDevchkAnswerId(devchkAnswerId);
                        taskExecRecord.setDevchkAnswerItemId(devchkAnswerItemId);
                        taskExecRecord.setNumContent(numContent);
                        taskExecRecord.setTextContent(textContent);
                        this.save(taskExecRecord);
                    }
                }
                // 附件保存
                List<String> attaUrlList = devchkAnswerSubmitVo.getAttaUrlList();
                if (attaUrlList != null) {
                    for (String attaUrl : attaUrlList) {
                        TaskExecRecordAtta taskExecRecordAtta = new TaskExecRecordAtta();
                        taskExecRecordAtta.setAttaUrl(attaUrl);
                        taskExecRecordAtta.setTaskMainId(taskMainId);
                        taskExecRecordAtta.setDeviceMainId(deviceMainId);
                        taskExecRecordAtta.setDevchkItemId(devchkItemId);
                        taskExecRecordAtta.setDevchkAnswerId(devchkAnswerId);
                        taskExecRecordAttaService.save(taskExecRecordAtta);
                    }
                }
            }
        }
        TaskDeviceDevchkItem taskDeviceDevchkItem = new TaskDeviceDevchkItem();
        taskDeviceDevchkItem.setTaskMainId(taskMainId);
        List<TaskDeviceDevchkItem> taskDeviceDevchkItemList = taskDeviceDevchkItemService.findList(taskDeviceDevchkItem);
        boolean closeFlag = true;
        for (TaskDeviceDevchkItem taskDeviceDevchkItem1 : taskDeviceDevchkItemList) {
            if (!TaskDeviceDevchkItem.Status.CLOSE.name().equals(taskDeviceDevchkItem1.getStatus())) {
                closeFlag = false;
                break;
            }
        }
        if (closeFlag) {
            if (taskMain != null) {
                taskMain.setTaskStatus(TaskDeviceDevchkItem.Status.CLOSE.name());
                taskMain.setCloseDate(new Date());
                taskMainService.saveSuper(taskMain);
            }
        }
    }

    public TaskExecRecord get(String id) {
        return super.get(id);
    }

    public List<TaskExecRecord> findList(TaskExecRecord taskExecRecord) {
        return super.findList(taskExecRecord);
    }

    public Page<TaskExecRecord> findPage(Page<TaskExecRecord> page, TaskExecRecord taskExecRecord) {
        return super.findPage(page, taskExecRecord);
    }

    @Transactional(readOnly = false)
    public void save(TaskExecRecord taskExecRecord) {
        super.save(taskExecRecord);
    }

    @Transactional(readOnly = false)
    public void delete(TaskExecRecord taskExecRecord) {
        super.delete(taskExecRecord);
    }

    public List<TaskExecRecord> listByTaskMainIds(List<String> taskMainIds) {
        if (taskMainIds == null || taskMainIds.size() == 0) {
            return Collections.EMPTY_LIST;
        }
        return this.dao.listByTaskMainIds(taskMainIds);
    }

}